$(function () {
    initEvent();
    /*输入框时时判断是否为空*/
    $('.main-t input').bind('input porpertychange',function(){
        var username = $.trim($("#username").val());
        var code = $.trim($("#code").val());
        var password = $.trim( $("#password").val());
        var password_ = $.trim($("#password_").val());
        if($(this).attr("id") == "username" && $(this).val() == ""){
            $("#J_getCode").attr("disabled",true)
        }else{
            $("#J_getCode").attr("disabled",false)
        }

        if(username != "" && code != "" && password != "" && password_ != ""){
            $("#FindpasswordClick").removeClass("disabled");
        }
    });
});

function initEvent() {
    $("#J_getCode").off('click').on('click', function () {
        tele_Phonecode();
    });
    $("#FindpasswordClick").off('click').on('click', function () {
        btn_Submit();
    });
}

/*短信验证码验证*/
function tele_Phonecode() {
    var username = $("#username").val();

    var result = common.checkForm().checkMobile(username, function (result, error) {
        if ('' == $.trim(username) || $.trim(username) == $("#username").attr('placeholder')) {
            falert('请输入手机号码');
            return false;
        } else if (result == false) {
            falert(error);
            return false;
        }
    }).value;

    if (result == true) {
        var datas = {
            interface:'register@sendcode', 
            phone: username
        };
        common.ajax({
            data: datas,
            beforeSend: function () {
                 $(".get-code").attr("disabled",true);
                common.clearCountDown($("#J_getCode"));
                common.countDown($("#J_getCode"));
            },
            success: function (data) {
                if (data.status == '2000') {
                  $("#J_getCode").attr('disabled',false);
                }
            }
        });

    }
    
}

function btn_Submit() {
    var username = $("#username").val();
    var code = $("#code").val();
    var password = $("#password").val();
    var password_ = $("#password_").val();

    if ('' == $.trim(username) || $.trim(username) == $("#username").attr('placeholder')) {
        falert('请输入手机号码');
        return false;
    } else if ('' == $.trim(code) || $.trim(code) == $("#code").attr('placeholder')){
        falert('请输入短信验证码');
        return false;
    } else if ('' == $.trim(password) || $.trim(password) == $("#password").attr('placeholder')) {
        falert('请输入密码');
        return false;
    } else if ('' == $.trim(password_) || $.trim(password_) == $("#password_").attr('placeholder')) {
        falert('请输入确认密码');
        return false;
    }

    var result = common.checkForm().checkMobile(username, function (result, error) {
        if (result == false) {
            falert(error);
            return false;
        }
    }).checkPwd(password, function (result, error) {
        if (result == false) {
            falert(error);
            return false;
        }
    }).checkPwd(password_, function (result, error) {
        if (result == false) {
            falert(error);
            return false;
        } else if(password != password_){
            falert('两次密码不一致，请重新输入');
            return false;
        }
    }).value;

    if (result == true) {
        var datas = {
            interface:'register@resetPwd', 
            username: username, 
            code:code,
            password: password,
            password2: password_
        };
        common.ajax({
            data: datas,
            success: function (data) {
                if (data.status == '2000') {
                    falert('重置成功','/pay/paylogin');
                } else if (data.status == '5000') {
                    falert(data.msg);
                }
            }
        });
    }
}
